<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
*/
namespace core;
defined('BY_JYA') or exit('error');
class db_tools{
	public function db_table_schemas($table) {
		$dump = "DROP TABLE IF EXISTS {$table};\n";
		$sql = "SHOW CREATE TABLE {$table}";
		$row = pdo_fetch($sql);
		$dump .= $row['Create Table'];
		$dump .= ";\n\n";
		return $dump;
	}
	public function db_table_insert_sql($tablename, $start=0, $size=300) {
		$data = '';
		$tmp = '';
		$sql = "SELECT * FROM {$tablename} LIMIT {$start}, {$size}";
		$result = pdo_fetchall($sql);
		if (!empty($result)) {
			foreach ($result as $row) {
				$tmp .= '(';
				foreach ($row as $k => $v) {
					$value = str_replace(array('\\', "\0", "\n", "\r", "'", '"', "\x1a"), array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'), $v);
					$tmp .= "'" . $value . "',";
				}
				$tmp = rtrim($tmp, ',');
				$tmp .= "),\n";
			}
			$tmp = rtrim($tmp, ",\n");
			$data .= "INSERT INTO {$tablename} VALUES \n{$tmp};\n";
			$datas = array(
					'data' => $data,
					'result' => $result,
			);
			return $datas;
		} else {
			return false;
		}
	}
	public function system_database_backup($name,$volume_sizes=1) {
		$path = ROOT_AT.'backup/'.$name.'/';
		$reduction = array();
		if (!is_dir($path)) {
			return false;
		}
		$now = $path . "/volume-{$volume_sizes}.sql";
		if (!is_file($now)) {
			return false;
		}	
		return $now;
	}
	function system_database_volume_restore($volume_name) {
		if (empty($volume_name) || !is_file($volume_name)) {
			return false;
		}
		$sql = file_get_contents($volume_name);
		pdo_run($sql);
		return true;
	}

}